import { sizeToPxNumber, useThemeStore } from "@/stores/theme"
import VariableName from "@/stores/theme/variable-name"

export class WindowPosition {
  left: number
  top: number
  right: number
  bottom: number

  constructor(left: number, top: number, right: number, bottom: number) {
    this.left = left
    this.top = top
    this.right = right
    this.bottom = bottom
  }

  setAll(position: WindowPosition) {
    this.left = position.left
    this.top = position.top
    this.right = position.right
    this.bottom = position.bottom
    return this
  }

  offset(event: MouseEvent) {
    return new WindowPosition(
      event.clientX - this.left,
      event.clientY - this.top,
      window.innerWidth - event.clientX - this.right,
      window.innerHeight - event.clientY - this.bottom
    )
  }

  toState(): any {
    const obj: any = {}
    obj.left = this.left
    obj.right = this.right
    obj.top = this.top
    obj.bottom = this.bottom
    return obj
  }

  static fromState(obj: any) {
    return new WindowPosition(obj.left, obj.top, obj.right, obj.bottom)
  }

  public static createFullscreenPosition() {
    return new WindowPosition(-5, -5, -5, -5)
  }

  public static createMaximizePosition() {
    const themeStore = useThemeStore()
    const topBarSize = themeStore.getVariable(VariableName.TopBar.size, sizeToPxNumber)
    const offsetDockSize = themeStore.getVariable(VariableName.Dock.size, sizeToPxNumber)
    const dockPlacement = themeStore.getVariable(VariableName.Dock.placement)
    if (dockPlacement === "left") {
      return new WindowPosition(offsetDockSize - 5, topBarSize - 5, -5, -5)
    } else if (dockPlacement === "right") {
      return new WindowPosition(-5, topBarSize - 5, offsetDockSize - 5, -5)
    } else {
      return new WindowPosition(-5, topBarSize - 5, -5, offsetDockSize - 5)
    }
  }
}
